package com.ruoyi.web.controller.controllers;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.provider.domain.AhctDefineDamageProject;
import com.ruoyi.provider.domain.AhctImage;
import com.ruoyi.provider.service.AhctDefineDamageProjectService;
import com.ruoyi.provider.service.AhctImageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/*
 * 定损项目列表
 */

@RestController
@RequestMapping("/loss")
public class AhctDefineProjectController extends BaseController {

    @Resource
    private AhctDefineDamageProjectService ahctDefineDamageProjectService;

    @Resource
    private AhctImageService ahctImageService;

    //获取定损尾部列表
    @GetMapping("/getDamageProperty")
    public AjaxResult getDamageProperty(@RequestParam String case_number){

        List<AhctDefineDamageProject> itemQuantity = ahctDefineDamageProjectService.getDamageProperty(case_number);
        List<AhctImage> imgList;
        imgList = ahctImageService.getImage(case_number);

        Map<Integer, List<String>> img = imgList.stream().collect(Collectors.toMap(AhctImage::getProject_id, each -> Collections.singletonList(each.getImage_url()), (value1, value2) -> {
            List<String> union = new ArrayList<>(value1);
            union.addAll(value2);
            return union;

        }));

        itemQuantity.forEach(x->x.setImgList(img.get(x.getId())));
        return success(itemQuantity);
    }

    //新增定损项目列表 + 照片   修改、删除操作也都是在这个接口实现
    @PostMapping("/insertList")
    public AjaxResult getDam(@RequestBody AhctDefineDamageProject ahctDefineDamageProject){

        //删除全部项目+图片,修改时接口也走这里,进行删除,全部重新新增
        ahctDefineDamageProjectService.deleteList(ahctDefineDamageProject);

        //新增项目列表
        ahctDefineDamageProjectService.insertDamage(ahctDefineDamageProject,ahctDefineDamageProject.getItemQuantity());

        //新增定损详情图片
        ahctImageService.insertIma(ahctDefineDamageProject);

        return success();
    }
}
